#### Figure 1

# Clear
rm(list=ls())


# Install and load required packages
if (!require("igraph")) install.packages("igraph")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("ggrepel")) install.packages("ggrepel")
if (!require("ggraph")) install.packages("ggraph")
if (!require("dplyr")) install.packages("dplyr")
if (!require("haven")) install.packages("haven")

# Working directory
setwd("~/Dropbox/Paraguay/1_data/7_replication_dataverse")

# Load land data
tmh_final <- read_dta("data/land.dta")

# Keep only land received during the dictatorship
tmh_c <- tmh_final %>%
  filter(year < 1989) %>%
  select(lastname, department, district, tmh_ha, tmh_n)

# Load links for San Juan Nepomuceno municipality 
load("data/sjn_links.RData")


# Clean lastnames and standardize capitalization
sjn <- sjn %>%
  mutate(
    lastname1 = gsub("_", " ", lastname1),
    lastname2 = gsub("_", " ", lastname2),
    lastname1 = tools::toTitleCase(lastname1),
    lastname2 = tools::toTitleCase(lastname2)
  )

# Get ill-gotten land data for SJN
sjn_land <- subset(tmh_c, district == "san juan nepomuceno") %>%
  subset(select = c(lastname)) %>%
  as.data.frame() %>%
  mutate(lastname = tools::toTitleCase(lastname))

# Create unique lastnames set and land data
unique_lastnames <- union(unique(sjn$lastname1), unique(sjn$lastname2))
land_data <- data.frame(lastname = unique_lastnames)
land_data$land <- 0
land_data$land[land_data$lastname %in% sjn_land$lastname] <- 1

# Create graph
g <- graph_from_data_frame(sjn, directed = FALSE)
g <- simplify(g)

# Set node attributes
V(g)$land <- land_data$land
V(g)$land <- factor(V(g)$land, levels = c("0", "1"), labels = c("No", "Yes"))

# Calculate eigenvector centrality
eigen_centrality <- eigen_centrality(g)$vector
median_centrality <- median(eigen_centrality)
V(g)$high_centrality <- ifelse(eigen_centrality > median_centrality, 
                               'Above median', 'Below median')

# Hide names for nodes without land
`%ni%` <- Negate(`%in%`)
V(g)$name <- ifelse(V(g)$name %ni% sjn_land$lastname, "", V(g)$name)

# Set visualization options
options(ggrepel.max.overlaps = 20)

# Seed for reproducibility
set.seed(1511)

# Create plot
sjn_plot <- ggraph(g, layout = 'nicely') +
  geom_edge_link(alpha = 0.2, color = "gray") +
  geom_node_point(aes(colour = land, shape = high_centrality), size = 2.6) +
  geom_text_repel(
    aes(x = x, y = y, label = name),
    box.padding = 0.8,    
    force = 1,           
    force_pull = 0,      
    max.overlaps = 35,   
    segment.size = 0.2,  
    size = 4,         
    family = "Times New Roman",
    segment.alpha = 0.6  
  ) +
  scale_shape_manual(
    name = "Eigenvector Centrality",
    values = c('Below median' = 16, 'Above median' = 15)
  ) +
  scale_colour_manual(
    name = "Ill-Gotten Lands",
    labels = c("No", "Yes"),
    values = c("darkgray", "darkred")
  ) +
  theme_graph() +
  theme(text = element_text(family = "Times New Roman", size = 15)) + coord_cartesian(clip = "off") 

# Save plot in figures folder
ggsave("figures/figure1.pdf", 
       plot = sjn_plot,
       width = 11, height = 9,
       device = cairo_pdf)


